package handler

import (
	"New-lesson-part2/custom_error"
	"New-lesson-part2/internal"

	"New-lesson-part2/product_web/req"
	"New-lesson-part2/proto/pb"
	"context"

	"github.com/gin-gonic/gin"

	"go.uber.org/zap"

	"net/http"
	"strconv"
)

func ProductListHandler(c *gin.Context) {
	var condition pb.ProductConditionReq
	minPriceStr := c.DefaultQuery("minPrice", "0")
	minPrice, err := strconv.Atoi(minPriceStr)
	if err != nil {
		zap.S().Error("minPrice is error")
		c.JSON(http.StatusOK, gin.H{"msg": custom_error.ParameterError})
		return
	}
	maxPriceStr := c.DefaultQuery("maxPrice", "0")
	maxPrice, err := strconv.Atoi(maxPriceStr)
	if err != nil {
		zap.S().Error("maxPrice is error")

		c.JSON(http.StatusOK, gin.H{"msg": custom_error.ParameterError})
		return
	}
	condition.MinPrice = int32(minPrice)
	condition.MaxPrice = int32(maxPrice)
	categoryIdStr := c.DefaultQuery("categoryId", "0")
	categoryId, err := strconv.Atoi(categoryIdStr)
	if err != nil {
		zap.S().Error("categoryId is error")
		c.JSON(http.StatusOK, gin.H{"msg": custom_error.ParameterError})
	}
	condition.CategoryId = int32(categoryId)
	brandIdStr := c.DefaultQuery("brandId", "0")
	brandId, err := strconv.Atoi(brandIdStr)
	if err != nil {
		zap.S().Error("brandId is error")
		c.JSON(http.StatusOK, gin.H{"msg": custom_error.ParameterError})
	}
	condition.BrandId = int32(brandId)

	isPopStr := c.DefaultQuery("isPop", "0")
	if isPopStr == "1" {
		condition.IsPop = true
	}
	isNewStr := c.DefaultQuery("isNew", "0")
	if isNewStr == "1" {
		condition.IsNew = true
	}
	pageNoStr := c.DefaultQuery("pageNo", "0")
	pageNo, err := strconv.Atoi(pageNoStr)
	if err != nil {
		zap.S().Error("pageNo is Error")
		c.JSON(http.StatusOK, gin.H{"msg": custom_error.ParameterError})
		return
	}
	pageSizeStr := c.DefaultQuery("pageSize", "0")
	pageSize, err := strconv.Atoi(pageSizeStr)
	if err != nil {
		zap.S().Error("pageSize is Error")
		c.JSON(http.StatusOK, gin.H{"msg": custom_error.ParameterError})
		return
	}
	condition.PageNo = int32(pageNo)
	condition.PageNo = int32(pageSize)
	keyWord := c.DefaultQuery("keyWord", "")
	condition.KeyWord = keyWord

	res, err := internal.ProductClient.ProductList(context.Background(), &condition)
	if err != nil {
		zap.S().Error("获取产品列表失败")
		c.JSON(http.StatusOK, gin.H{"msg": "获取产品列表失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":   "",
		"total": res.Total,
		"data":  res.ProductItemRes,
	})

}
func AddHandler(c *gin.Context) {
	var productReq req.ProductReq
	err := c.ShouldBindJSON(&productReq)
	if err != nil {
		zap.S().Error(err)
		c.JSON(http.StatusOK, gin.H{
			"msg": custom_error.ParameterError,
		})
		return
	}
	req2pb := ConvertProductReq2pb(productReq)
	productRes, err := internal.ProductClient.CreateProduct(context.Background(), req2pb)
	if err != nil {
		zap.S().Error(err)
		c.JSON(http.StatusOK, gin.H{
			"msg": custom_error.ProductCreateDataFailed,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":            "",
		"productItemRes": productRes,
	})

}
func DetailHandler(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		zap.S().Error(custom_error.ParameterError)
		c.JSON(http.StatusOK, gin.H{
			"msg": custom_error.ParameterError,
		})
		return
	}
	res, err := internal.ProductClient.GetProductDetail(context.Background(), &pb.ProductItemReq{Id: int32(id)})
	if err != nil {
		zap.S().Error(custom_error.GetDetailToFailed)
		c.JSON(http.StatusOK, gin.H{
			"msg": custom_error.GetDetailToFailed,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "",
		"data": res,
	})
}
func DeleteHandler(c *gin.Context) {

	idStr := c.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		zap.S().Error(custom_error.ParameterError)
		c.JSON(http.StatusOK, gin.H{
			"msg": custom_error.ParameterError,
		})
		return
	}
	_, err = internal.ProductClient.DeleteProduct(context.Background(), &pb.ProductDelItem{Id: int32(id)})
	if err != nil {
		zap.S().Error(custom_error.ParameterError)
		c.JSON(http.StatusOK, gin.H{
			"msg": custom_error.ParameterError,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg": "",
	})
}

func UpdateHandler(c *gin.Context) {
	var productReq req.ProductReq
	err := c.ShouldBindJSON(&productReq)
	if err != nil {
		zap.S().Error(err)
		c.JSON(http.StatusOK, gin.H{
			"msg": custom_error.ParameterError,
		})
		return
	}
	req2pb := ConvertProductReq2pb(productReq)
	_, err = internal.ProductClient.UpdateProduct(context.Background(), req2pb)
	if err != nil {
		zap.S().Error(err)
		c.JSON(http.StatusOK, gin.H{
			"msg": err,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg": "",
	})
}
func ConvertProductReq2pb(req req.ProductReq) *pb.CreateProductItem {
	res := &pb.CreateProductItem{
		Id:         req.Id,
		CategoryId: req.CategoryId,
		BrandId:    req.BrandId,
		Selling:    req.Selling,
		IsPop:      req.IsPop,
		IsNew:      req.IsNew,
		Name:       req.Name,
		SN:         req.SN,
		FavNum:     req.FavNum,
		SoldNum:    req.SoldNu,
		Price:      float32(req.Price),
		RealPrice:  float32(req.RealPrice),
		ShortDesc:  req.ShortDesc,
		CoverImage: req.CoverImages,
		Images:     req.Images,
		DescImage:  req.DescImages,
	}
	return res
}
